package com.example.administrator.ttphonehelper.battery;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;

import com.example.administrator.ttphonehelper.R;

public class TabHostActivity extends TabActivity {
    TabHost mTabHost;
    TabWidget mTabWidget;
    View mView1;
    View mView2;
    ImageView mIVIcon;
    TextView mTVText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_host);
        init();
    }

    private void init() {
        mTabHost = getTabHost();
        mTabWidget=getTabWidget();
        //添加选项卡
        TabHost.TabSpec tab1 = mTabHost.newTabSpec("info");
        //设置页签视图
        mView1 = View.inflate(this, R.layout.tabhost_indicator_view,null);
        mView1.setBackgroundResource(R.mipmap.top_bg);
        mIVIcon = (ImageView) mView1.findViewById(R.id.tab_icon);
        mTVText = (TextView) mView1.findViewById(R.id.tab_text);
        mTVText.setText("电池信息");
        mIVIcon.setImageResource(R.mipmap.battery_manager);
        tab1.setIndicator(mView1);
        //设置页签点击事件
        Intent intent1 = new Intent(this,BatteryManager1Activity.class);
        tab1.setContent(intent1);

        mTabHost.addTab(tab1);

        //添加第二个选项卡
        TabHost.TabSpec tab2 = mTabHost.newTabSpec("info");
        mView2 = View.inflate(this, R.layout.tabhost_indicator_view,null);
        mIVIcon = (ImageView) mView2.findViewById(R.id.tab_icon);
        mTVText = (TextView) mView2.findViewById(R.id.tab_text);
        mTVText.setText("电池充电");
        mView2.setBackgroundResource(R.mipmap.top_bg);
        mIVIcon.setImageResource(R.mipmap.battery_fix);
        tab2.setIndicator(mView2);

        Intent intent2 = new Intent(this,BatteryManager2Activity.class);
        tab2.setContent(intent2);

        mTabHost.addTab(tab2);
        //切换背景
        mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                //当前选中的页签的索引
                int currIndex = mTabHost.getCurrentTab();
                changeTab(currIndex);
            }
            
        });
    }

    private void changeTab(int currIndex) {
        for (int i = 0; i <mTabWidget.getTabCount() ; i++) {
            View view = mTabWidget.getChildAt(i);
            view.setBackgroundResource(i==currIndex? R.mipmap.disk_bg_pressed: R.mipmap.top_bg);
        }
    }
}
